$(function () {
  $.initAdd();
});

(function($) {
	$.extend({
		initAdd : function() {
			layui.use(['upload', 'form'], function() {
				var upload = layui.upload;
				var form = layui.form;
				
				var uploadInst = upload.render({
        			elem: "#upload-btn", //绑定元素
                    url: "http://localhost:9140/model/tempFileUpload", //上传接口
                    size: 10240,
                    accept: "file",
                    field: "file",
                    done: function(result) {
                    	if(result.result) {
                    		$("#tempKey").val(result.data);
                            $.layui_alert(result.message);
                            $("#upload-btn").remove();
                    	}
                    }
        		});
				form.on("submit(form-submit)", function(data) {
        		var load_index = layer.load(1, {shade:0.4});
        		var field = data.field; //获取提交的字段
            $.ajax({
                type: "POST",
                dataType: "json",
                url: "http://localhost:9140/model/modelAdd" ,
                data: field,
                success: function (result) {
                    $.layui_alert(result.message);
                    layer.close(load_index);
                },
                error : function() {
                    $.layui_alert("提交请求异常！");
                    layer.close(load_index);
                }
            });
        });
				form.render();
			});
		}
	});
	
})(jQuery);